#ifndef TASKAPI_H
#define TASKAPI_H

#include <QObject>
#include <QString>
#include <QFileInfo>
#include <QIODevice>

#include <QNetworkAccessManager>
#include <QNetworkRequest>
#include <QNetworkReply>
#include <QEventLoop>
#include <QHttpMultiPart>
#include <QHttpPart>

#include <QJsonDocument>
#include <QJsonArray>
#include <QJsonObject>

#include <QVector>

#include "Tools/Helper.h"

/**
 * @brief The TaskApi class
 * 给AsyncRunner使用，完成api回调部分
 *
 * 注意这些api需要进行详细的测试，否则大概率会崩溃
 *
 */
namespace TaskApi {
    // TODO 后面抽离到一些文本配置文件，这个接口需要频繁更改
    const static QString BASE_URL = "http://114.55.245.149:8051";
    // const static QString BASE_URL = "http://127.0.0.1:9000";
    const static QString TASK_GEN = BASE_URL + "/gateway/task/generate";
    const static QString TASK_POST = BASE_URL + "/gateway/task/post";
    const static QString Task_IN = BASE_URL + "/gateway/task/in";
    const static QString TASK_QUERY = BASE_URL + "/gateway/task/query";
    const static QString TASK_DOWNLOAD = BASE_URL + "/gateway/task/download";
    const static QString RESULT_QUERY = BASE_URL + "/gateway/result/query";


    const static QString DEFAULT_USER_NAME = "HelloWorld";
    // 记得读源代码
    static inline QString genOptions(){
        QJsonObject json;
        json.insert("enhance", 0);
        json.insert("seg", 1);
        json.insert("grade", 0);

        QJsonDocument document;
        document.setObject(json);
        QByteArray bArray = document.toJson(QJsonDocument::Compact);
        return QString(bArray);
    };
    const static QString DEFAULT_RUNNING_OPTIONS = TaskApi::genOptions();

}

// 同步方式实现
namespace SyncTaskApi {
    // TODO 注意返回500的话,服务器通常会返回额外的str信息，这里没写需要优化

    typedef struct ApiResult{
        int code;
        QString result;
        QString errMsg;
    }RemoteApiResult;

    typedef struct ApiTaskProgress : RemoteApiResult{
        int taskStatus;
        QString taskMsg;
        double taskProgrss;
    }RemoteApiProgress;


    RemoteApiResult createRemoteTask(const QString userName);
    // TODO 将文件分组上传
    RemoteApiResult uploadFileToTask(const QString taskId, const QVector<QString>& fileList);
    RemoteApiResult putTask(const QString taskId, const QString options);
    ApiTaskProgress taskQuery(const QString taskId);
    RemoteApiResult resultQuery(const QString taskId, const QString fileName, const QString options);
}

#endif // TASKAPI_H
